package com.kidback.bbloc259.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;

import com.kidback.bbloc259.base.BaseActivity;
import com.kidback.bbloc259.databinding.WebViewActivityBinding;
import com.kidback.bbloc259.util.StatusBarUtils;

public class WebViewActivity extends BaseActivity<WebViewActivityBinding> {

    public static void start(Context context, String title, String url) {
        Intent intent = new Intent(context, WebViewActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

        intent.putExtra("title", title);
        intent.putExtra("url", url);

        context.startActivity(intent);
    }

    @Override
    public WebViewActivityBinding getViewBinding() {
        return WebViewActivityBinding.inflate(inflater);
    }

    @Override
    protected void initView() {
        StatusBarUtils.setStatusBarColor(activity, 0xff715af3);

        viewBinding.titleBar.setTintColor(Color.WHITE);

        String title = getIntent().getStringExtra("title");
        viewBinding.titleBar.setTitle(title);
        viewBinding.titleBar.setOnBackClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        String url = getIntent().getStringExtra("url");
        viewBinding.webView.loadUrl(url);

        //声明WebSettings子类
        WebSettings webSettings = viewBinding.webView.getSettings();

//如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);

//设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

//缩放操作
        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件

//其他细节操作
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式

    }

    @Override
    protected void loadData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        ViewGroup parent = (ViewGroup) viewBinding.webView.getParent();
        parent.removeView(viewBinding.webView);
        viewBinding.webView.destroy();
    }

}
